package com.youyun56.gps.android.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class LocationHelper {

    public static Location getLocation(String sid, int bid, int nid, String mobileCountryCode, RadioType radioType)
            throws IOException, JSONException {
        Log.i("sid:", "" + sid);
        Log.i("bid:", "" + bid);
        Log.i("nid:", "" + nid);
        ArrayList<CellIDInfo> cellIds = new ArrayList<CellIDInfo>();
        CellIDInfo info = new CellIDInfo();
        info.cellId = bid;
        info.locationAreaCode = nid;
        info.mobileNetworkCode = sid;
        info.mobileCountryCode = mobileCountryCode;
        // info.mobileCountryCode = tm.getNetworkOperator().substring(3, 5);
        info.radioType = radioType;
        cellIds.add(info);
        Log.d("cellId:", "" + info.cellId);
        Log.d("locationAreaCode:", "" + info.locationAreaCode);
        Log.d("mobileNetworkCode:", info.mobileNetworkCode);
        Log.d("mobileCountryCode:", info.mobileCountryCode);
        return callGear(cellIds);
    }

    private static Location callGear(ArrayList<CellIDInfo> cellID) throws IOException, JSONException {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://www.google.com/loc/json");
        JSONObject holder = new JSONObject();
        holder.put("version", "1.1.0");
        holder.put("host", "maps.google.com");
        holder.put("home_mobile_country_code", cellID.get(0).mobileCountryCode);
        holder.put("home_mobile_network_code", cellID.get(0).mobileNetworkCode);
        holder.put("radio_type", cellID.get(0).radioType.name().toLowerCase());
        holder.put("request_address", true);
        holder.put("address_language", "zh_CN");

        JSONObject data = null;
        JSONObject current_data = null;
        JSONObject adress = null;
        JSONArray array = new JSONArray();
        current_data = new JSONObject();
        current_data.put("cell_id", cellID.get(0).cellId);
        current_data.put("location_area_code", cellID.get(0).locationAreaCode);
        current_data.put("mobile_country_code", cellID.get(0).mobileCountryCode);
        current_data.put("mobile_network_code", cellID.get(0).mobileNetworkCode);
        current_data.put("age", 0);
        current_data.put("signal_strength", -60);
        current_data.put("timing_advance", 550);
        array.put(current_data);

        holder.put("cell_towers", array);

        StringEntity se = new StringEntity(holder.toString());
        Log.e("Location send", holder.toString());
        post.setEntity(se);
        HttpResponse resp = client.execute(post);

        HttpEntity entity = resp.getEntity();

        BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
        StringBuffer sb = new StringBuffer();
        String result = br.readLine();
        while (result != null) {
            Log.e("Locaiton reseive", result);
            sb.append(result);
            result = br.readLine();
        }

        data = new JSONObject(sb.toString());
        Log.d("-", sb.toString());
        if (data.has("location")) {
            JSONObject location = data.getJSONObject("location");
            if (location.has("latitude") && location.has("longitude")) {
                return new Location(location.getDouble("latitude"), location.getDouble("longitude"));
            }
        }
        return null;
        // data = (JSONObject) data.get("location");
        // adress = (JSONObject) data.get("address");
        //
        // String city = (String) adress.get("city");
        // String street = (String) adress.getString("street") + (String)
        // adress.get("street_number");
        //
        // region = (String) adress.get("region");
        // return loc;
    }

}
